<template>
    <orh-trees>    
        <Tree :data="trees" :load-data="loadData" @on-select-change="getNodes" slot="trees"></Tree>
        <orh-sub-table pages :pageDate="pageDate" @on-page-change="Information" slot="content">
            <Table :data="InsuranceList" :columns="columns1" highlight-row ref="currentRowTable" slot="table"></Table>
        </orh-sub-table>
    </orh-trees>
</template>

<script>
    export default {    
        data() {
            return {
                InsuranceList: [],
                content: null,
                isShow: false,
                dataIds: null,
                datailId: null,
                titles: null,
                listUrl: null,
                type: "one",
                prompt: false,
                loading: true,
                cont: "您确定要删除这些数据吗",
                del: null,
                eclassId: null,
                searchOptionSelected: "student_name",
                typelist: [{
                        value: "student_name",
                        label: "学生"
                    },
    
                ],
                extend_filter: {
                    searchtype: null,
                    searchvalue: null,
                    semester_id: null,
                    group_id: null,
                    subject_group_id: null,
                    status: null
                },
                pageDate: {
                    total: 0,
                    current_page: 1,
                    per_page: 0
                },
                trees: [],
                show: 0,
                gradeId: null,
                isShow: false,
                columns1: [    
                    {
                        title: "用户id",
                        width: 200,
                        key: "user_id"
                    },
                    {
                        title: "教师姓名",
                        width: 200,
                        render: (h, params) => {
                            if (params.row.teacher != null) {
                                let teacher_name = params.row.teacher
                                    .teacher_name.zh;
                                return h("div", teacher_name);
                            }    
                        }
                    },
                    {
                        title: "所任课程",
                        render: (h, params) => {
                            if (params.row.course != null) {
                                let course_name = params.row.course.course_name.zh;    
                                return h("div", course_name);
                            }
    
                        }
                    },    
                ],
            };
        },
        methods: {
            load() {
                this.$http.get("api/base/v1/get/grade/list").then(val=>{
                    this.trees = this.process(val.data)
                })
            },
            // 处理数据
            process(data) {
                let arr = [];
                data.forEach(item => {
                    if (item.eclass_name === undefined) {
                        item.title = item.grade_name.zh;
                        item.grade_id = item.id;
                        (item.loading = false), (item.children = []);
                    } else {
                        item.title = item.eclass_name.zh;
                        item.eclass_id = item.value = item.eclass_id;
                        item.grade_id = item.grade_id
                    }
                    item.value = item.id;
                    arr.push(item);
                });
                return arr;
            },
            // 异步加载班级
            loadData(item, callback) {
                this.$http.get("api/eclass/v1/eclass/grade_eclass?grade_id=" + item.id).then(val=>{
                    callback(this.process(val.data));
                })
            },
            getNodes(item) {
                if (item[0].eclass_id == undefined) {
                } else {
                    this.eclassId = item[0].eclass_id
                }
            },
    
            Information(index) {
                let extend_filter = this.extend_filter;
                let url = "";
                for (var i in extend_filter) {
                    if (extend_filter[i] != null) {
                        url += i + "=" + extend_filter[i] + "&";
                    }
                }
                url = url.trim("&");
                if (this.eclassId != null) {
                    this.$http.get("api/course/v1/course_arrange/eclass_teacher/" + this.eclassId + '?page=' + index + "&" + url).then(val => {
                        this.InsuranceList = val.data
                        this.pageDate.per_page = val.pages.per_page;
                        this.pageDate.total = val.pages.total;
                        this.pageDate.current_page = val.pages.current_page;
                    }).catch(err=>{
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                    })
                }    
                this.$Message.destroy(); //加载完数据动画结束
            },
        },
        mounted() {
            this.load();
            this.Information()
        },
        watch: {
    
            eclassId() {
                this.Information()
            }
        }
    };
</script>




